<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>学生目录</title>
    <style>
        body{
            margin: 0px;
            padding: 0;
        }
        ul{
            margin: 0px;
            padding:0px;
        }
        li{
            list-style: none;
            border-bottom:1px solid #eee ;
        }
        span{
            display: inline-block;
            width: 300px;
            height: 50px;
            line-height: 60px;
            font-size: 18px;
            text-align: left;
            color: #666;
        }
        #l span{
            display: inline-block;
            width: 295px;
            height: 50px;
            line-height: 60px;
            font-size: 18px;
            text-align: left;
        }
        input ,select{
            width: 245px;
            height: 30px;
            margin: 0px;
            font-size: 20px;
            float: left;
            margin-right: 30px;
            border-radius: 3px;
            border: 1px solid #888;
        }
        button{
            width: 245px;
            height: 35px;
            text-align: center;
            background: dodgerblue;
            color: #fff;
            font-size: 20px;
            border: none;
            outline: none;
            cursor: pointer;
            border-radius:3px ;
        }
        input{

            padding-left: 6px;

        }
        .kong select{
            height: 36px;
        }
        .kong{
            overflow: hidden;
            margin-bottom: 25px;
        }
        .head{
            width: 100%;
            height: 350px;
            text-align: center;
            padding: 80px;
            box-sizing: border-box;
            background: #eee;
            margin-bottom: 50px;
        }
        .head h1{
            margin-top: 20px;
            font-size: 60px;
        }
        .head p{
            font-size: 20px;
            color: #423f3d;
        }
        .jia{
            width: 1100px;
            margin: auto;
        }
        #ul{
            margin-bottom: 50px;
        }
    </style>
</head>
<body>

<div class="head">
    <h1>学员信息表</h1>
    <p>1804</p>
</div>


<div class="jia">

<div class="kong">
    <input type="text" id="ming" placeholder="姓名">
    <select id="xing">
        <option value ="男">男</option>
        <option value ="女">女</option>

    </select>
    <input type="number" placeholder="年龄" id="lin" >

    <button id="ji">添加</button>

</div>
    <h2>列表</h2>

    <li id="l">
        <span id="n">姓名</span>
        <span id="s">性别</span>
        <span id="a">年龄</span>
    </li>
<ul id="ul">


</ul>
</div>
<script>
    function loadXMLDoc()
    {
        var xmlhttp;
        if (window.XMLHttpRequest)
        {
            //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
            xmlhttp=new XMLHttpRequest();
        }
        else
        {
            // IE6, IE5 浏览器执行代码
            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange=function()
        {
            if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
//                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
                var json = JSON.parse(xmlhttp.responseText);
                console.log(json);
                json = json.data.reverse()

                for (var i=0;i<json.length;i++){
                    var li =document.createElement("li")
                    var sn=document.createElement("span")
                    sn.innerHTML =json[i].studentName
                    var ss=document.createElement("span")
                    ss.innerHTML =json[i].sex

                    var sa=document.createElement("span")
                    sa.innerHTML =json[i].age

                    li.appendChild(sn)
                    li.appendChild(ss)
                    li.appendChild(sa)
                    document.querySelector("#ul").appendChild(li)
                }
            }
        }
        xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/student",true);
        xmlhttp.send()                           ;
    }
//    loadXMLDoc()

    var dian=document.querySelector("#ji")
    ji.addEventListener('click',function () {
        var ming=document.querySelector("#ming").value;
        console.log(ming)

        var xing=document.querySelector("#xing").value;
        console.log(xing)

        var lin=document.querySelector("#lin").value;
        console.log(lin)


            var xmlhttp;
            if (window.XMLHttpRequest)
            {
                //  IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
                xmlhttp=new XMLHttpRequest();
            }
            else
            {
                // IE6, IE5 浏览器执行代码
                xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange=function()
            {
                if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    console.log(xmlhttp.responseText)

                    document.querySelector("#ul").innerHTML=""

                    loadXMLDoc()
                }
            }
            xmlhttp.open("GET","http://localhost/ajaxApi/public/index.php/index/student/addStudent?studentName="+ming+"&sex="+xing+"&age="+lin,true);
            xmlhttp.send()                           ;

            document.querySelector("#ming").value=""
            document.querySelector("#lin").value=""


    })

    loadXMLDoc()


</script>

</body>
</html>